package modeleditor.editor;

import linechart.Linechart;
import mutiplechart.Mutiplechart;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.custom.ScrolledComposite;
//import org.eclipse.swt.layout.GridData;
//import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.EditorPart;
import org.eclipse.ui.part.FileEditorInput;






import mutiplechart.Topochart;



import ganttchart.Ganttchart;
import highlowchart.Highlowchart;

import javax.swing.*;

import java.awt.*;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.experimental.chart.swt.ChartComposite;
import org.jfree.ui.TextAnchor;
import org.jfree.chart.ChartPanel;

import paretochart.Paretochart;
import piechart.Piechart;
import scatterchart.Scatterchart;
import spiderwebchart.Spiderwebchart;
import topochart.TheTopochart;
import areachart.Areachart;
import barchart.Barchart;
import barchart.Bar;
import barchart.Category;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;


public class myeditor extends EditorPart {
	public static final String ID = "ModelEditor.editor";
	private FileEditorInput finput;	
	private JFreeChart chart;
	private List<JFreeChart> charts;
	private int flag = 0;
	public Topochart topo;
	public TheTopochart ttopo;
	
	
	 @Override
	  public void init(IEditorSite site, IEditorInput input)
	      throws PartInitException {		 
		 FileEditorInput finput = (FileEditorInput)input;
		 IFile f = finput.getFile();
		 String filepath = f.getLocation().toOSString();
		 ResourceSet resourceSet = new ResourceSetImpl();
		 	 
		 Resource resource = resourceSet.createResource(URI.createFileURI(filepath));
		 try{
		 resource.load(null);
		 
		 //System.out.println(resource.getContents().get(0));
		 } catch (Exception e){
			 e.printStackTrace();
			 //System.out.println("error");
		 }
		 //Barchart mybarchart = (Barchart) resource.getContents().get(0);
		 //System.out.println(mybarchart.getXname());
		// System.out.println(finput);
	    if (!(finput instanceof FileEditorInput)) {
	      throw new RuntimeException("Wrong input");
	    }

	    FileEditorInput new_name = (FileEditorInput) finput;
	    this.finput = (FileEditorInput) finput;
	    setSite(site);
	    setInput(finput);
	    System.out.println(resource.getContents().get(0));
	    flag = 0;
	    if(resource.getContents().get(0) instanceof Barchart){
	    	setPartName("Barchart");
	    	drawbarchart barchart = new drawbarchart((Barchart)resource.getContents().get(0));
	    	chart = barchart.createBarChart(barchart.getDataset());
	    	System.out.println(resource.getContents().get(0));
	    } else if(resource.getContents().get(0) instanceof Piechart){
	    	setPartName("Piechart");
	    	drawpiechart piechart = new drawpiechart((Piechart)resource.getContents().get(0));
	    	chart = piechart.createPieChart(piechart.getDataSet());
	    	System.out.println(resource.getContents().get(0));	    	
	    } else if(resource.getContents().get(0) instanceof Linechart){
	    	setPartName("Linechart");
	    	drawlinechart linechart = new drawlinechart((Linechart)resource.getContents().get(0));
	    	chart = linechart.createChart(linechart.createDataset());
	    	System.out.println(resource.getContents().get(0));
	    } else if(resource.getContents().get(0) instanceof Areachart){
	    	setPartName("Areachart");
	    	drawareachart areachart = new drawareachart((Areachart)resource.getContents().get(0));
	    	chart = areachart.createChart(areachart.createDataset());
	    	System.out.println(resource.getContents().get(0));
	    } else if(resource.getContents().get(0) instanceof Ganttchart){
	    	setPartName("Ganttchart");
	    	drawganttchart ganttchart = new drawganttchart((Ganttchart)resource.getContents().get(0));
	    	chart = ganttchart.createChart(ganttchart.createDataset());
	    	System.out.println(resource.getContents().get(0));
	    } else if(resource.getContents().get(0) instanceof Paretochart){
	    	setPartName("Paretochart");
	    	drawparetochart paretochart = new drawparetochart((Paretochart)resource.getContents().get(0));
	    	chart = paretochart.createChart(paretochart.createDatasets());
	    	System.out.println(resource.getContents().get(0));
	    } else if(resource.getContents().get(0) instanceof Spiderwebchart){
	    	setPartName("Spiderwebchart");
	    	drawspiderwebchart spiderwebchart = new drawspiderwebchart((Spiderwebchart)resource.getContents().get(0));
	    	chart = spiderwebchart.createChart(spiderwebchart.createDataset());
	    	System.out.println(resource.getContents().get(0));
	    } else if(resource.getContents().get(0) instanceof Highlowchart){
	    	setPartName("Highlowchart");
	    	drawhighlowchart highlowchart = new drawhighlowchart((Highlowchart)resource.getContents().get(0));
	    	chart = highlowchart.createChart(highlowchart.createDataset());
	    	System.out.println(resource.getContents().get(0));
	    } else if(resource.getContents().get(0) instanceof Scatterchart){
	    	setPartName("Scatterchart");
	    	drawscatterplotchart scatterchart = new drawscatterplotchart((Scatterchart)resource.getContents().get(0));
	    	scatterplotdataset dataset = new scatterplotdataset((Scatterchart)resource.getContents().get(0));
	    	chart = scatterchart.createChart(dataset);
	    	System.out.println(resource.getContents().get(0));
	    } else if(resource.getContents().get(0) instanceof Mutiplechart){
	    	flag = 1;
	    	setPartName("Mutiplechart");
	    	drawmutiplechart mutiplechart = new drawmutiplechart((Mutiplechart)resource.getContents().get(0));
	    	charts = mutiplechart.creatmutiplechart();
	    	topo = mutiplechart.createtopo();
	    	//Barchart chart1 = (Barchart) ((Mutiplechart)resource.getContents().get(0)).getCharts().get(0);
	    	//Piechart chart2 = (Piechart) ((Mutiplechart)resource.getContents().get(0)).getCharts().get(1);
	    	//drawbarchart barchart = new drawbarchart(chart1);
	    	//charts.add(barchart.createBarChart(barchart.getDataset()));
	    	//drawpiechart piechart = new drawpiechart(chart2);
	    	//charts.add(piechart.createPieChart(piechart.getDataSet()));
	    	System.out.println(((Mutiplechart)resource.getContents().get(0)).getCharts().get(0));
	    } else if(resource.getContents().get(0) instanceof TheTopochart){
	    	flag = 2;
	    	setPartName("Topochart");
	    	ttopo = (TheTopochart) resource.getContents().get(0);
	    }
	    
	    //this.barchart = (Barchart) resource.getContents().get(0);
	    //this.categories = barchart.getCategories();
	    
	    //person = MyModel.getInstance().getPersonById(this.input.getId());
	    //setPartName("Person ID: " + person.getId());
	  }

	
	 
	  @Override
	  public void createPartControl(Composite parent) {	
		  /*
		  drawlinechart a = new drawlinechart("abc");
		  Composite composite = new Composite(parent, SWT.EMBEDDED | SWT.NO_BACKGROUND);
		  final Frame frame =  SWT_AWT.new_Frame(composite);
		  frame.add(a.createDemoPanel());
		  */
		  
		  if(flag == 1){
			  /*
			  GridLayout g = new GridLayout();
			  g.numColumns = 2; 
			  g.makeColumnsEqualWidth = true; 
			  
			  g.marginBottom = 0;
		      g.marginTop = 0;
		      g.marginLeft = 0;
		      g.marginRight = 0;
		      
		      GridData gridData = new GridData(); 
		      gridData.verticalAlignment = GridData.FILL; 
		      gridData.grabExcessHorizontalSpace = true; 
		      gridData.grabExcessVerticalSpace = true; 
		      gridData.horizontalAlignment = GridData.FILL; 
		      parent.setLayoutData(gridData);
		      parent.setSize(800, 600);
			  parent.setLayout(g);
			  */
			  /*
			  final GridLayout gridLayout_2 = new GridLayout();
			  gridLayout_2.marginHeight = 9;
			  gridLayout_2.verticalSpacing = 9;
			  parent.setLayout(gridLayout_2);
			  */
			  /*
			  final Composite composite = new Composite(parent, SWT.MULTI);
			  composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
			  final GridLayout gridLayout = new GridLayout();
			  gridLayout.numColumns = 2;
			  composite.setSize(600, 500);
			  composite.setLayout(gridLayout);		
			  */
			  int a = charts.size();
			  int b =2;
			  int height = ((double)a/(double)b)>(a/b)?a/b+1:a/b;
			  ScrolledComposite composite = new ScrolledComposite(parent, SWT.EMBEDDED | SWT.NO_BACKGROUND | SWT.V_SCROLL);	
	          composite.setExpandHorizontal(true);
	          composite.setExpandVertical(true);
	          composite.setMinWidth(600);
	          composite.setMinHeight(300*height);
			  Composite mainComposite = new Composite(composite,SWT.EMBEDDED);
		      composite.setContent(mainComposite);
			  final Frame frame =  SWT_AWT.new_Frame(mainComposite);
			  frame.setLayout(new GridLayout(0,2));				  
			  for(int i=0; i<charts.size(); i++){				  
				  final ChartPanel c = new ChartPanel(charts.get(i));	
				  final JPanel j = (JPanel)c;
				  /*
				  GridLayout g = new GridLayout();
				  g.numColumns = 2; 
				  composite.setLayout(g);
				  final Frame frame =  SWT_AWT.new_Frame(composite);
				  frame.add(c);
				  */			  
				  SwingUtilities.invokeLater(new Runnable() {			  
		              public void run() {
						  frame.add(j);	
		              }			  
				  });		  
			  				  			  
				  //final ChartComposite frame = new ChartComposite(composite, SWT.NONE, charts.get(i), true);
			  }
			  
			  if(topo != null){
				  final JPanel swingPanel = new JPanel();
				  SwingUtilities.invokeLater(new Runnable() {			  
		              public void run() {
		        		  multi_topochart test = new multi_topochart(swingPanel, topo);
		        		  test.vis.run("color");
		        		  test.vis.run("layout");
		        		  test.ZoomToFitByStart();
		                  frame.add(swingPanel);  
		              }			  
				  });	
			  }
			  
		  } else if(flag == 0){
			  final ChartComposite frame = new ChartComposite(parent, SWT.NONE, chart, true);
		  } else if(flag == 2){
			  
			  Composite composite = new Composite(parent, SWT.EMBEDDED | SWT.NO_BACKGROUND);
			  final Frame frame =  SWT_AWT.new_Frame(composite);
			  final JPanel swingPanel = new JPanel();
			  SwingUtilities.invokeLater(new Runnable() {			  
	              public void run() {
	        		  prefuse test = new prefuse(swingPanel, ttopo);
	        		  test.vis.run("color");
	        		  test.vis.run("layout");
	        		  test.ZoomToFitByStart();
	                  frame.add(swingPanel);  
	              }			  
			  });	
			  
		  }
		  
	  }

	  @Override
	  public void doSave(IProgressMonitor monitor) {
	    // person.getAddress().setCountry(text2.getText());
	  }

	  @Override
	  public void doSaveAs() {
	  }

	  @Override
	  public boolean isDirty() {

	    return false;
	  }

	  @Override
	  public boolean isSaveAsAllowed() {
	    return false;
	  }

	  @Override
	  public void setFocus() {
	  }

}
